package category

import (
	"errors"
	"go-demo/core/db"
	"gorm.io/gorm"
)

type Category struct {
	gorm.Model
	ID         int    `json:"id"`
	Name       string `json:"name"`
	Pid        int    `json:"pid"`
	Status     int    `json:"status"`
	CreateTime int64  `json:"create_ime"`
	UpdateTime int64  `json:"update_time"`
}

// TableName 设置表名
func (Category) TableName() string {
	return "demo_category"
}

// FindById 根据id获取分类
func FindById(id int) Category {
	category := Category{}
	db.DB.Where("id = ?", id).Find(&category)
	return category
}

// CheckIsExist 根据id判断分类是否存在
func CheckIsExist(id int) (category Category, err error) {
	result := db.DB.Model(&category).Select("*").Where("id = ?", id).First(&category)
	if errors.Is(result.Error, gorm.ErrRecordNotFound) {
		err = errors.New("数据不存在")
		return
	}
	return
}
